package com.tonann.web.dangerous;

import com.alibaba.fastjson2.JSONObject;
import okhttp3.MediaType;
import okhttp3.Response;

import java.util.HashMap;
import java.util.Map;

public class DangerousToken {
    public static String token;

    public static String getToken() {
        if (token == null) {
            MediaType mediaType = MediaType.parse("application/json");
            Map<String, String> map = new HashMap<>();
            map.put("userName", "13301010101");
            map.put("password", "Kaka@0101");
            String url = "https://dev.schemical.kakashine.com/openapi/token";
            try {
                JSONObject jsonObject = OkHttpUtil.getInstance()
                        .doPost(url, map);
                //响应体
                if (jsonObject != null) {
                    DangerousUserInfo userInfo = new DangerousUserInfo();
                    userInfo.setData((String) jsonObject.get("data"));
                    userInfo.setErrCode((Integer) jsonObject.get("errCode"));
                    userInfo.setSuccess((Boolean) jsonObject.get("success"));
                    if (userInfo.getErrCode() == 200 && userInfo.getSuccess()) {
                        token = userInfo.getData();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return token;
    }
}
